<template>
  <div>
    <el-form
      ref="editForm"
      :model="editForm"
      class="pd20"
      label-width="120px"
      label-position="right"
      size="medium"
      :disabled="!!goodsId"
    >
      <div
        v-for="(item, index) in selectOrder"
        :key="index"
        style="margin-left: -92px"
      >
        <!-- <i class="el-icon-delete lg" /><br> -->
        <!-- <i class="el-icon-plus blue" /> -->
        <!-- <span class="blue" @click="addGoodsList">添加</span> -->
      </div>
      <el-form-item label="商品名称:">
        <el-input
          v-model="editForm.goodsName"
          autocomplete="off"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="库存数量:">
        <el-input-number
          v-model="editForm.inventory"
          autocomplete="off"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="商品封面图片:">
        <el-upload
          action="https://ypsupply.knoweasetech.com/sys/goods/updatePhoto"
          list-type="picture-card"
          accept=".png, .jpg"
          :headers="uploadHeaders"
          :file-list="headPictureList"
          :on-success="(res) => handleSuccess(res, 'headPicture')"
          :limit="5"
          multiple
        >
          <i slot="default" class="el-icon-plus" />
          <!-- <div slot="file" slot-scope="{ editForm }">
            {{ editForm.headPicture }}
            <img
              class="el-upload-list__item-thumbnail"
              :src="editForm.headPicture"
              alt=""
            >
          </div> -->
        </el-upload>
      </el-form-item>
      <el-form-item label="详情图片:">
        <el-upload
          action="https://ypsupply.knoweasetech.com/sys/goods/updatePhoto"
          list-type="picture-card"
          accept=".png, .jpg"
          :headers="uploadHeaders"
          :file-list="pictureList"
          :on-success="(res) => handleSuccess(res, 'pictures')"
          :limit="20"
          multiple
        >
          <i slot="default" class="el-icon-plus" />
          <!-- <div slot-scope="{ editForm }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="editForm.picture"
              alt=""
            >
          </div> -->
        </el-upload>
      </el-form-item>
      <el-form-item label="商品等级">
        <goods-rank-list
          :rank-id="editForm.goodsRankId"
          @change="goodsRankChange"
        />
      </el-form-item>
      <el-form-item label="商家编码:">
        <el-input
          v-model="editForm.sellerCode"
          autocomplete="off"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="商品原价:">
        <el-input
          v-model="editForm.originalPrice"
          autocomplete="off"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="上架状态:">
        <el-select v-model="editForm.status" placeholder="请选择" @change="statusChange">
          <el-option
            label="上架"
            :value="1"
          />
          <el-option
            label="下架"
            :value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addGoods, getGoodsInfoById } from '@/api/goods'
import { getToken } from '@/utils/auth'
const statusTextMap = {
  1: '上架',
  2: ' 下架'
}
const editForm = {
  goodsName: '',
  inventory: '',
  goodsRankId: null,
  sellerCode: '',
  status: '',
  statusText: '',
  pictures: [],
  headPicture: []
}
export default {
  components: {},
  props: {
    selectOrder: {
      type: Array,
      default: function() {
        return []
      }
    },
    goodsId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      dialog: false,
      editForm,
      // 上传
      uploadHeaders: {
        Authorization: getToken(),
        timeout: 50000
      },
      pictureList: [],
      headPictureList: []
    }
  },
  watch: {
    goodsId(v) {
      if (v) {
        this.getGoodsInfoById(v)
        this.editForm.statusText = statusTextMap[this.editForm.status]
      } else {
        this.editForm = editForm
        this.pictureList = []
        this.headPictureList = []
      }
    }
  },
  created() {},
  methods: {
    // 发布
    async submitForm() {
      const params = this.editForm
      params.pictures = params.pictures.join(',')
      params.headPicture = params.headPicture.join(',')
      await addGoods(params)

      this.$message.success('新增商品成功')
      this.$emit('onGoodsAdd')
      this.editForm = {}
    },
    handleSuccess(res, type) {
      this.editForm[type].push(res.data.enclosure)
    },
    /**
     * 根据id获取商品详情
     */
    async getGoodsInfoById(id) {
      const { data } = await getGoodsInfoById(id)
      this.pictureList =
        (data.pictures &&
          data.pictures.split(',').map((item) => {
            return { name: '商品图', url: item }
          })) ||
        []
      this.headPictureList =
        (data.headPicture &&
          data.headPicture.split(',').map((item) => {
            return { name: '商品图', url: item }
          })) ||
        []
      this.editForm = data
    },
    /**
     * 等级变化
     */
    goodsRankChange(id, row) {
      this.editForm.goodsRankId = id
    },
    /**
     * 上架状态变化
     */
    statusChange(row) {
      // this.editForm.statusText = row
    },
    showSuccessMsg() {
      this.$message({
        message: '上传成功',
        type: 'success'
      })
    },
    showFailMsg() {
      this.$message.error('上传失败')
    }
  }
}
</script>
